package com.crimerank;

import java.util.ArrayList;
import java.util.List;

import com.crimerank.data.ParcelableGeoPoint;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;

public class Map extends MapActivity {

	MapView mapView;
	List<Overlay> mapOverlays;
	Drawable dot;
	Drawable bubble;
	Drawable bubble_g;
	MapItemizedOverlay itemizedOverlay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);

		Bundle extras = getIntent().getExtras();
		String mapType = extras.getString("mapType");

		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		MapController mc = mapView.getController();

		mapOverlays = mapView.getOverlays();

		// Create the point markers to use.
		dot = this.getResources().getDrawable(
				R.drawable.ic_maps_indicator_current_position);
		bubble = this.getResources().getDrawable(R.drawable.bubble);
		bubble_g = this.getResources().getDrawable(R.drawable.bubble_g);

		// Initialize the grouping of points to overlay.
		itemizedOverlay = new MapItemizedOverlay(bubble);

		// Get current location from the Intent extras.
		Bundle loc = extras.getBundle("loc");
		Double lat = loc.getDouble("lat") * 1E6;
		Double lon = loc.getDouble("lon") * 1E6;
		GeoPoint currentLoc = new GeoPoint(lat.intValue(), lon.intValue());

		// Add the current location to the overlay.
		OverlayItem currentLocOverlayItem = new OverlayItem(currentLoc,
				"Line1", "Line2");
		itemizedOverlay.addOverlayCenter(currentLocOverlayItem, dot);

		// Center the map on the current point and set zoom level.
		// This will only be the final settings if a single point is being
		// displayed.
		mc.setCenter(currentLoc);
		mc.setZoom(17);

		if (mapType.equals("geoPoint")) {

			// Center the map at the centroid of the points and
			// set view to show them all

			ArrayList<ParcelableGeoPoint> pointsExtra = getIntent()
					.getParcelableArrayListExtra("points");

			Long totalLat = (long) currentLoc.getLatitudeE6();
			Long totalLon = (long) currentLoc.getLongitudeE6();

			for (ParcelableGeoPoint p : pointsExtra) {
				GeoPoint point = p.getGeoPoint();
				totalLat += point.getLatitudeE6();
				totalLon += point.getLongitudeE6();
				OverlayItem overlayItem = new OverlayItem(point, "Line1",
						"Line2");
				itemizedOverlay.addOverlayCenterBottom(overlayItem, bubble);

			}

			Integer x = (int) ((double) totalLat / itemizedOverlay.size());
			Integer y = (int) ((double) totalLon / itemizedOverlay.size());
			GeoPoint newCenter = new GeoPoint(x, y);

			mc.setCenter(newCenter);
			mc.zoomToSpan(itemizedOverlay.getLatSpanE6(),
					itemizedOverlay.getLonSpanE6());

		} else if (mapType.equals("geoRoute")) {

			// Plot the set of points provided as one Path, a route.
			ArrayList<ParcelableGeoPoint> pointsExtra = getIntent()
					.getParcelableArrayListExtra("points");
			mapOverlays.add(new MapOverlay(pointsExtra));

			mc.setCenter(new GeoPoint(extras.getInt("latCenter"), extras
					.getInt("lonCenter")));
			mc.zoomToSpan(extras.getInt("latSpan"), extras.getInt("lonSpan"));

			// Get the stop location from the Intent extras.
			Bundle stopLocBundle = extras.getBundle("stopLoc");
			lat = stopLocBundle.getDouble("lat") * 1E6;
			lon = stopLocBundle.getDouble("lon") * 1E6;
			GeoPoint stopLoc = new GeoPoint(lat.intValue(), lon.intValue());

			// Add the stop location to the overlay.
			OverlayItem stopLocOverlayItem = new OverlayItem(stopLoc, "Line1",
					"Line2");
			itemizedOverlay.addOverlayCenterBottom(stopLocOverlayItem, bubble);

			// Get the bus location from the Intent extras.
			ArrayList<ParcelableGeoPoint> busPosExtra = getIntent()
					.getParcelableArrayListExtra("busPos");
			if (busPosExtra.size() == 0) {
				Toast toast = Toast
						.makeText(
								getApplicationContext(),
								"No buses currently traveling this route in the chosen direction.",
								Toast.LENGTH_LONG);
				toast.show();
			}
			for (ParcelableGeoPoint p : busPosExtra) {
				OverlayItem busLocOverlayItem = new OverlayItem(
						p.getGeoPoint(), "Line1", "Line2");
				itemizedOverlay.addOverlayCenterBottom(busLocOverlayItem,
						bubble_g);
			}

		}

		mapOverlays.add(itemizedOverlay);

	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
